#import "dfhgwhruebvhrheymournrural.h"
#import "dfhgwhruebvhrheyadmitultimate.h"
#import "dfhgwhruebvhrheyluggagerecorder.h"
#import "dfhgwhruebvhrheyslotcounter.h"
#import "dfhgwhruebvhrheygarbageequator.h"
#import "IQKeyboardManager.h"
#import "dfhgwhruebvhrheyobsceneinject.h"
#import "dfhgwhruebvhrheymergefry.h"
#import "dfhgwhruebvhrheymagnificenttunnel.h"
#import "dfhgwhruebvhrheycataloguetheory.h"
#import "dfhgwhruebvhrheycataloguetheory.h"
#import "dfhgwhruebvhrheybangorganismViewController.h"
#import "dfhgwhruebvhrheydiscourageconstituent.h"
#import "dfhgwhruebvhrheynastydispositionViewController.h"
#import "dfhgwhruebvhrheycalmprophet.h"
#import "dfhgwhruebvhrheyconfusionfuneral.h"
#import "dfhgwhruebvhrheyaveragebid.h"
#import <CoreLocation/CoreLocation.h>
#import "dfhgwhruebvhrheylaserbeforehandViewController.h"
#import "dfhgwhruebvhrheysemesterfuriousViewController.h"
#import "dfhgwhruebvhrheyavenuetyre.h"
@interface dfhgwhruebvhrheynastydispositionViewController ()<CLLocationManagerDelegate, dhgfywegwygeydwtransportmortal>
@property (nonatomic, strong) dispatch_group_t group;
@property (nonatomic, strong) dfhgwhruebvhrheygarbageequator *repayment;
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (weak, nonatomic) IBOutlet UIView *containerView;
@property (weak, nonatomic) IBOutlet UIView *oneView;
@property (weak, nonatomic) IBOutlet UIView *twoView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *top;
@property (weak, nonatomic) IBOutlet UIImageView *logo;
@property (weak, nonatomic) IBOutlet UILabel *appName;
@property (nonatomic, strong) dfhgwhruebvhrheyobsceneinject *initialView;
@property (nonatomic, strong) dfhgwhruebvhrheymergefry *examineView;
@property (nonatomic, strong) dfhgwhruebvhrheydiscourageconstituent *withdrawalView;
@property (nonatomic, strong) dfhgwhruebvhrheycalmprophet *repayView;
@property (nonatomic, strong) dfhgwhruebvhrheyconfusionfuneral *offlineCodeView;
@end
@implementation dfhgwhruebvhrheynastydispositionViewController {
	NSArray *banners;
}
- (dfhgwhruebvhrheyobsceneinject *)initialView {
	if (_initialView == nil) {
		_initialView = [[NSBundle mainBundle] loadNibNamed:@"dfhgwhruebvhrheyobsceneinject" owner:nil options:nil].firstObject;
	}
	return _initialView;
}
- (dfhgwhruebvhrheymergefry *)examineView {
	if (_examineView == nil) {
		_examineView = [[NSBundle mainBundle] loadNibNamed:@"dfhgwhruebvhrheymergefry" owner:nil options:nil].firstObject;
	}
	return _examineView;
}
- (dfhgwhruebvhrheydiscourageconstituent *)withdrawalView {
	if (_withdrawalView == nil) {
		_withdrawalView = [[NSBundle mainBundle] loadNibNamed:@"dfhgwhruebvhrheydiscourageconstituent" owner:nil options:nil].firstObject;
	}
	return _withdrawalView;
}
- (dfhgwhruebvhrheycalmprophet *)repayView {
	if(_repayView == nil) {
		_repayView = [[NSBundle mainBundle] loadNibNamed:@"dfhgwhruebvhrheycalmprophet" owner:nil options:nil].firstObject;
	}
	return _repayView;
}
- (dfhgwhruebvhrheyconfusionfuneral *)offlineCodeView {
	if (_offlineCodeView == nil) {
		_offlineCodeView = [[NSBundle mainBundle] loadNibNamed:@"dfhgwhruebvhrheyconfusionfuneral" owner:nil options:nil].firstObject;
	}
	return _offlineCodeView;
}
- (UIStatusBarStyle)preferredStatusBarStyle {
	return UIStatusBarStyleLightContent;
}
- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	[[IQKeyboardManager sharedManager] setEnable:NO];
    self.navigationController.navigationBarHidden = YES;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = NO;
	[self loadData];
}
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
	[[IQKeyboardManager sharedManager] setEnable:YES];
    self.navigationController.navigationBarHidden = NO;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
}
- (void)viewDidLoad {
	[super viewDidLoad];
	[self loadUI];
	[self loadRiskData];
	[self startLocationService];
}
- (void)loadRiskData {
	if ([dfhgwhruebvhrheyoptionsaving isLogin]) {
		[[NSUserDefaults standardUserDefaults] registerDefaults:@{@"date": [NSDate date]}];
		NSDate *date = [[NSUserDefaults standardUserDefaults] objectForKey:@"date"];
		if ([[NSCalendar currentCalendar] isDateInToday:date]) {
			[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"date"];
			[[NSUserDefaults standardUserDefaults] registerDefaults:@{@"count":@dhgfywegwygeydwmortalharmony}];
			NSInteger count = [[NSUserDefaults standardUserDefaults] integerForKey:@"count"];
			if (count != 0) {
				count --;
				[dfhgwhruebvhrheyoptionsaving uploadRiskDataByCatchId:@"3" andApplyId:nil withDetailId:nil];
				[[NSUserDefaults standardUserDefaults] setInteger:count forKey:@"count"];
			}
		}
	}
}
- (void)startLocationService {
	self.locationManager = [[CLLocationManager alloc] init];
	self.locationManager.delegate = self;
    [self.locationManager requestWhenInUseAuthorization];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager requestLocation];
}
- (void)loadHomeData {
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwlambbreast path:homeApi isEncrypt:NO withParams:nil success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		strongSelf.homeModel = [dfhgwhruebvhrheyadmitultimate yy_modelWithJSON:json];
		[[NSUserDefaults standardUserDefaults] setObject:strongSelf.homeModel.service_one forKey:dhgfywegwygeydwharmonynasty];
		[[NSUserDefaults standardUserDefaults] setObject:strongSelf.homeModel.service_two forKey:dhgfywegwygeydwnastydisposition];
		dispatch_group_leave(strongSelf.group);
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		dispatch_group_leave(strongSelf.group);
	}];
}
- (void)loadPopShowData {
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwlambbreast path:showPopDataApi isEncrypt:YES withParams:nil success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		strongSelf.showPopData = [dfhgwhruebvhrheycataloguetheory yy_modelWithJSON:json];
		dispatch_group_leave(strongSelf.group);
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		dispatch_group_leave(strongSelf.group);
	}];
}
- (void)loadData {
	__weak typeof(self) weakSelf = self;
	if (self.group == nil) {
		self.group = dispatch_group_create();
	}
	dispatch_group_enter(self.group);
	[self loadHomeData];
	dispatch_group_enter(self.group);
	[self loadPopShowData];
	[dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
	dispatch_group_notify(self.group, dispatch_get_main_queue(), ^{
		__strong typeof(weakSelf) strongSelf = weakSelf;
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
		[strongSelf bindData];
	});
}
- (void)bindData {
	if (self.showPopData.code == 200) {
		[self showHomePopView];
	}else {
		if (self.showPopData.error.length) {
			[dfhgwhruebvhrheyoptionsaving.keyWindow show:self.showPopData.error];
		}
	}
    [self offLineGetMoney: NO];
    [self isRepaymentStatus:NO];
	if (self.homeModel.code == 200) {
		[self layoutUIByHomeData];
	}else {
		if (self.homeModel.error.length) {
			[dfhgwhruebvhrheyoptionsaving.keyWindow show:self.homeModel.error];
		}
	}
}
- (void)isRepaymentStatus:(BOOL)status {
    if (status) {
        self.top.constant = 12;
        self.logo.hidden = YES;
        self.appName.hidden = YES;
        self.navigationController.navigationBarHidden = NO;
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(13, 0, dhgfywegwygeydwdispositionvisualize-26, 44)];
        titleLabel.text = @"Repayment";
        titleLabel.font = [UIFont semiBold:20];
        titleLabel.textColor = [UIColor whiteColor];
        self.navigationItem.titleView = titleLabel;
    }else {
        self.top.constant = 112;
        self.logo.hidden = NO;
        self.appName.hidden = NO;
        self.navigationController.navigationBarHidden = YES;
    }
}
- (void)offLineGetMoney:(BOOL)status {
    if (status) {
        self.top.constant = 12;
        self.logo.hidden = YES;
        self.appName.hidden = YES;
        self.navigationController.navigationBarHidden = NO;
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(13, 0, dhgfywegwygeydwdispositionvisualize-26, 44)];
        titleLabel.text = @"Withdraw money";
        titleLabel.font = [UIFont semiBold:20];
        titleLabel.textColor = [UIColor whiteColor];
        self.navigationItem.titleView = titleLabel;
    }else {
        self.top.constant = 112;
        self.logo.hidden = NO;
        self.appName.hidden = NO;
        self.navigationController.navigationBarHidden = YES;
    }
}
- (void)getRepaymentData {
	if (![dfhgwhruebvhrheyoptionsaving isLogin]) {
		return;
	}
    [self isRepaymentStatus:YES];
	[dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwlambbreast path:repaymentDataApi isEncrypt:YES withParams:nil success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
		__strong typeof(weakSelf) strongSelf = weakSelf;
		dfhgwhruebvhrheygarbageequator *repayment = [dfhgwhruebvhrheygarbageequator yy_modelWithJSON:json];
		strongSelf.repayment = repayment;
		if (repayment.code == 200) {
			strongSelf.repayView.showRepayPlan = repayment.is_ins_repay == 1;
			[strongSelf.repayView bindRepayment:repayment];
		}else {
			[dfhgwhruebvhrheyoptionsaving.keyWindow show:repayment.error];
		}
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
	}];
}
- (void)loadUI {
    self.oneView.layer.cornerRadius = 12;
    self.twoView.layer.cornerRadius = 12;
    self.twoView.backgroundColor = second_color.dhgfywegwygeydwlearneddesignate;
    self.oneView.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.view.bounds;
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(0, 1);
    gradientLayer.colors = @[(__bridge  id)primary_color.dhgfywegwygeydwlearneddesignate.CGColor, (__bridge  id)second_color.dhgfywegwygeydwlearneddesignate.CGColor];
    gradientLayer.locations = @[@(0.2), @(1.0)];
    [self.view.layer insertSublayer:gradientLayer atIndex:0];
}
- (void)showHomePopView {
	NSLog(@"是否需要弹窗");
}
- (void)machineAuditRequest {
	NSLog(@"请求机审");
	[dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwawaitlamb path:machineAuditApi isEncrypt:YES withParams:nil success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
		dfhgwhruebvhrheysternideology *model = [dfhgwhruebvhrheysternideology yy_modelWithJSON:json];
		if (model.code == 200) {
			[strongSelf loadData];
		}else {
			[dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
		}
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
	}];
}
- (void)layoutUIByHomeData {
	__weak typeof(self) weakSelf = self;
	[self.containerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
	if (self.homeModel.status == 0 || self.homeModel.status == 1 || self.self.homeModel.status == 9) {
		[self.containerView addSubview:self.initialView];
		[self.initialView bindData:self.homeModel];
	}
	if (self.homeModel.status == 2 || self.homeModel.status == 4 || self.homeModel.status == 8) {
		[self.examineView bindData:self.homeModel andCallBack:^{
			__strong typeof(weakSelf) strongSelf = weakSelf;
			if (strongSelf.homeModel.status == 2 && strongSelf.homeModel.refuse_status != 1 && strongSelf.homeModel.first_loan > 1) {
				[self loadData];
			}
		}];
		[self.containerView addSubview:self.examineView];
	}
	if (self.homeModel.status == 3 || (self.homeModel.status == 5 && self.homeModel.user_loan_status == 1) || self.homeModel.status == 6 || self.homeModel.status == 10 ) {
		[self.withdrawalView bindData:self.homeModel andCallBack:^{
			__strong typeof(weakSelf) strongSelf = weakSelf;
			if (strongSelf.homeModel.status == 6) {
				[strongSelf machineAuditRequest];
			}
		}];
		[self.containerView addSubview:self.withdrawalView];
	}
	if (self.homeModel.status == 5 && self.homeModel.user_loan_status == 0) {
		[self.repayView bindOverDayData:self.homeModel];
		[self.containerView addSubview:self.repayView];
		[self getRepaymentData];
		__weak typeof(self) weakSelf = self;
		[self.repayView showRepayPlanCallBack:^(int type) {
			__strong typeof(weakSelf) strongSelf = weakSelf;
			if (type == 1) {
				[strongSelf showRepayPlanPopView];
			}else {			
				[strongSelf repayAction];
			}
		}];
	}
	if (self.homeModel.status == 7) {
        [self offLineGetMoney:YES];
		__weak typeof(self) weakSelf = self;
		[self.containerView addSubview:self.offlineCodeView];
		[self.offlineCodeView bindData:self.homeModel andCallBack:^{
			__strong typeof(weakSelf) strongSelf = weakSelf;
			[strongSelf showOffLinePasswordPopView];
		}];
	}
}
- (void)showRepayPlanPopView {
	NSLog(@"显示还款计划弹窗");
	__weak typeof(self) weakSelf = self;
	dfhgwhruebvhrheyslotcounter *popView = [[NSBundle mainBundle] loadNibNamed:@"dfhgwhruebvhrheyslotcounter" owner:nil options:nil].firstObject;
	popView.frame = dfhgwhruebvhrheyoptionsaving.keyWindow.bounds;
	[dfhgwhruebvhrheyoptionsaving.keyWindow addSubview:popView];
	[popView bindRepayment:self.repayment andRepayClick:^{
		__strong typeof(weakSelf) strongSelf = weakSelf;
		[strongSelf repayAction];
	}];
}
- (void)loadOffLineCode:(NSString *)passwd {
	if (passwd.length != 6) return;
	NSMutableDictionary *params = [NSMutableDictionary dictionary];
	params[@"password"] = passwd;
	[dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwawaitlamb path:getOffLineCodeApi isEncrypt:YES withParams:params success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
		__strong typeof(weakSelf) strongSelf = weakSelf;
		dfhgwhruebvhrheymagnificenttunnel *model = [dfhgwhruebvhrheymagnificenttunnel yy_modelWithJSON:json];
		if (model.code == 200) {
			[strongSelf detailOffLinePage:model];
		}else {
			[dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
		}
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
	}];
}
- (void)detailOffLinePage:(dfhgwhruebvhrheymagnificenttunnel *)model {
	dfhgwhruebvhrheysemesterfuriousViewController *code = [[dfhgwhruebvhrheysemesterfuriousViewController alloc] init];
	code.codeModel = model;
	code.hidesBottomBarWhenPushed = YES;
	[self.navigationController pushViewController:code animated:YES];
}
- (void)repayAction {
	NSLog(@"去还款");
	dfhgwhruebvhrheylaserbeforehandViewController *repay = [[dfhgwhruebvhrheylaserbeforehandViewController alloc] init];
	repay.hidesBottomBarWhenPushed = YES;
	repay.repayment = self.repayment;
	[self.navigationController pushViewController:repay animated:YES];
}
- (IBAction)tapAction:(UITapGestureRecognizer *)sender {
    dfhgwhruebvhrheybangorganismViewController *webVC = [[dfhgwhruebvhrheybangorganismViewController alloc] init];
    webVC.isShowBottomView = NO;
    switch (sender.view.tag) {
        case 100: {
            NSLog(@"how to loan");
            [webVC loadURLString:howToLoanH5];
            webVC.navigationItem.title = @"How To Loan?";
        }
            break;
        case 200: {
            NSLog(@"loan requirement");
            [webVC loadURLString:loanTermsH5];
            webVC.navigationItem.title = @"Loan requirement";
        }
            break;
        default:
            break;
    }
    webVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:webVC animated:YES];
}
- (void)showOffLinePasswordPopView {
	dfhgwhruebvhrheyaveragebid *passwordView = [[dfhgwhruebvhrheyaveragebid alloc]initWithFrame:CGRectMake(0, 0, dhgfywegwygeydwdispositionvisualize, dhgfywegwygeydwchaselocality) WithTitle:@"Please enter the registration password"  WithButton:@[@"Cancel",@"Confirm"]];
	passwordView.passwordDelegate = self;
	if(![passwordView.TF becomeFirstResponder]) {
		[passwordView.TF becomeFirstResponder];
	}
	[self.view.window addSubview:passwordView];
}
- (void)viewDidLayoutSubviews {
	[super viewDidLayoutSubviews];
	if (self.containerView.subviews.count) {
		if ([self.containerView.subviews containsObject:self.initialView]) {
			[self.initialView mas_makeConstraints:^(MASConstraintMaker *make) {
				make.edges.equalTo(self.containerView);
			}];
		}
		if ([self.containerView.subviews containsObject:self.examineView]) {
			[self.examineView mas_makeConstraints:^(MASConstraintMaker *make) {
				make.edges.equalTo(self.containerView);
			}];
		}
		if ([self.containerView.subviews containsObject:self.withdrawalView]) {
			[self.withdrawalView mas_makeConstraints:^(MASConstraintMaker *make) {
				make.edges.equalTo(self.containerView);
			}];
		}
		if ([self.containerView.subviews containsObject:self.repayView]) {
			[self.repayView mas_makeConstraints:^(MASConstraintMaker *make) {
				make.edges.equalTo(self.containerView);
			}];
		}
		if ([self.containerView.subviews containsObject:self.offlineCodeView]) {
			[self.offlineCodeView mas_makeConstraints:^(MASConstraintMaker *make) {
				make.edges.equalTo(self.containerView);
			}];
		}
	}
}
# pragma mark : ============ dhgfywegwygeydwtransportmortal ========
-(void)inputPasswordView:(dfhgwhruebvhrheyaveragebid *)view withPasswordString:(NSString *)password WithButtonIndex:(NSInteger)buttonIndex {
    if(buttonIndex == 1) {
        if(password.length != 6) {
            return [dfhgwhruebvhrheyoptionsaving.keyWindow show:@"Please enter the complete password."];
        }
        [self loadOffLineCode:password];
    }
    [view hiddenAllPoint];
}
# pragma mark : ============ CLLocationManagerDelegate ========
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
	if (locations.count) {
		CLLocation *location = [locations lastObject];
		[[NSUserDefaults standardUserDefaults] setDouble:location.coordinate.latitude forKey:dhgfywegwygeydwvisualizeterritory];
		[[NSUserDefaults standardUserDefaults] setDouble:location.coordinate.longitude forKey:dhgfywegwygeydwterritoryexaggerate];
	}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(nonnull NSError *)error {
}
@end
